package com.li.onjava8.concurrent.cf.example.cake;

import com.li.onjava8.concurrent.utils.Nap;

import java.util.concurrent.CompletableFuture;

/**
 * 作为一个示例，说明如何使用CompletableFutures将一系列操作组合在一起，让我们模拟制作蛋糕的过程。
 * 1. 在第一个阶段中，我们准备并将成分混合成面糊
 * @author li
 * @date 2023/10/31 14:59
 */

public class Batter {
    static class Eggs {}
    static class Milk {}
    static class Sugar {}
    static class Flour {}

    static <T> T prepare(T ingredient) {
        new Nap(0.1);
        return ingredient;
    }

    static <T>CompletableFuture<T> prep(T ingredient) {
        return CompletableFuture
                .completedFuture(ingredient)
                .thenApplyAsync(Batter::prepare);
    }

    public static  CompletableFuture<Batter> mix() {
        CompletableFuture<Eggs> eggs = prep(new Eggs());
        CompletableFuture<Milk> milk = prep(new Milk());
        CompletableFuture<Sugar> sugar = prep(new Sugar());
        CompletableFuture<Flour> flour = prep(new Flour());

        CompletableFuture.allOf(eggs, milk, sugar, flour);
        new Nap(0.1); //Mixing time

        return CompletableFuture.completedFuture(new Batter());
    }

}
